<template>
  <div>
    <el-dialog :title="title" :visible.sync="visible" width="60%" append-to-body custom-class="import-dialog">
      <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" @submit.native.prevent>
          <el-form-item label="小区" prop="estateName">
            <el-input v-model="queryParams.estateName" placeholder="请输入小区" clearable style="width: 240px"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="楼栋" prop="buildingName">
            <el-input v-model="queryParams.buildingName" placeholder="请输入楼栋" clearable style="width: 240px"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="房间" prop="roomNumber">
            <el-input v-model="queryParams.roomNumber" placeholder="请输入房间" clearable style="width: 240px"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange" ref="table">
          <!-- <el-table-column label="房间编号" align="center" prop="houseId" /> -->
          <el-table-column label="楼盘" align="center" prop="estateName" />
          <el-table-column label="楼栋" align="center" prop="buildingName" />
          <el-table-column label="单元" align="center" prop="unitName" />
          <el-table-column label="房间号" align="center" prop="roomNumber" />
          <!-- <el-table-column label="周期类型" align="center" prop="apartmentType">
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.dict_unsubscribe_rule_type"
                :value="scope.row.apartmentType"
              />
            </template>
          </el-table-column> -->
          <!-- <el-table-column
            label="租金（元）"
            align="center"
            prop="roomNumber"
          />
          <el-table-column
            label="押金（元）"
            align="center"
            prop="roomNumber"
          /> -->
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="selectHosue(scope.row)">选择房屋</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
          @pagination="getList" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRoom } from "@/api/house/room";
export default {
  props: {
    filterId: {
      type: String,
      default: "",
    },
    title: {
      default: "选择房间",
    },
    value: {
      default: false,
    },
    //是否筛选空置房间
    vacancy: {
      type: Boolean,
      default: false,
    },
  },
  dicts: [],
  data() {
    return {
      visible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        estateName: undefined,
        buildingName: undefined,
        roomNumber: undefined,
        apartmentType: undefined,
        editStatus: undefined,
        apartmentState: undefined,
        updateBy: undefined,
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 总条数
      total: 0,
      //列表
      list: [],
    };
  },
  watch: {
    value(newVal, oldVal) {
      if (newVal) {
        this.init();
      }
      this.visible = newVal; // 监测value的变化，并赋值。
    },
    visible(val) {
      this.$emit("input", val); // 此处监测visible目的为关闭弹窗时，重新更换value值，注意emit的事件一定要为input。
    },
  },
  methods: {
    init() {
      if (this.vacancy) {
        this.queryParams.houseState = 2;
      }
      this.reset()
      this.getList();
    },
    handleQuery() {
      this.getList();
    },
    reset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        estateName: undefined,
        buildingName: undefined,
        roomNumber: undefined,
        apartmentType: undefined,
        editStatus: undefined,
        apartmentState: undefined,
        updateBy: undefined,
      }
      this.resetForm("queryForm");
    },
    resetQuery() {
      // this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查询房间列表 */
    getList() {
      this.loading = true;
      listRoom(this.queryParams).then((response) => {
        let list = response.rows.filter((item) => {
          return item.houseId != this.filterId;
        });
        this.list = list;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleSelectionChange() { },
    selectHosue(item) {
      this.$emit("success", item);
      this.close();
    },
    close() {
      this.resetQuery();
      this.visible = false;
    },
  },
};
</script>

<style lang="scss" scoped></style>
